accessibility
Macromedia Logo Upper Navigation Bar
 Help
Lower Navigation Bar
ProductsSupportDesigner DeveloperDownloadsStoreInternationalSite MapCompany
Home > Products > Director > Support > TechNote Index
Macromedia Director Support Center - TechNote

Conditional statements: how to use "if" statements

Product: Director
Platform: All
Versions: 7.0 and above
ID: 16207
How useful was this document?
less more

1

2

3

4

5

How can the document be improved? (300 characters or less - you will not receive a reply.)

A conditional statement is used to test the value of an expression and perform an action based on the result. A real world example of a conditional statement could be "If it is raining, I should bring my umbrella". The condition is whether or not it is raining. The action is whether or not I bring my umbrella. Conditional statements are invaluable when testing for specific scenarios in Director. For example, a conditional statement can test to see if a QuickTime movie is playing, and if it is not the Director movie will jump to a different frame.

'If' statements are most often used to test one condition. Nested if statements, which are discussed below, can be used to test more than one condition. 'Case' statements can test for two or more conditions. For more information about case statements, see Conditional statements: how to use "case" statements (TechNote16228).

If Statements
'If' statements can be incorporated into scripts or executed from the message window. 'If' statements have a specific structure. First the condition is needed, then an action is needed based on the result of the condition (whether it is true or not). Optionally, another action can be added for cases where the condition is not true.

By default, the condition is tested as true. The first example below can be translate to mean "if the condition (it is raining) is true, then (bring umbrella)".

The third example below can be translated to "If the condition (it is NOT raining) is true, then (don't bring an umbrella)". Review the structure and examples below:

structure
If (condition) then (action)
example
1: if (it is raining) then (bring umbrella )
2: if (it is raining) then (bring umbrella) else (don't bring umbrella)
3: if NOT (it is raining) then (don't bring umbrella)
4: if NOT (it is raining) then (don't bring umbrella) else (bring umbrella)

The following three statements test to see if x is equal to true. Although they all look different, they are testing for the same thing. Since they are all the same, the result will always be an alert box with the word "true". In the sample movie, each of these statements has a corresponding button.

> If x=true then alert "true"
> if x=1 then alert "true"
> if (x) then alert "true"

This next example can be attached to a button. In the sample movie the button labeled "full script example" corresponds to this script. An alert function is used, which, when executed, makes an alert box appear with the specified text. On a mouseUp event, the script tests to see if x is true, in which case an alert box pops up with the message "x=true". If x is not equal to true, an alert box pops up with the message "x=false".

global x
on mouseUp
if x=true then
alert "x=true"
else
alert "x=false"
end if
end

Nested 'if' statements
Nested 'if' statement can be used to test for more than two conditions. If an if statement becomes more involved, a case statement may be a more effective scripting method.

The terms "and" as well as "or" may be used in 'if' statements. The example script below shows the use of "and".

The following script checks the value of the global variables x and y. Based on the values of these global variables, one of 4 alerts will appear. This script is attached to the button labeled "nested statement" in the sample movie.

global x
global y
on mouseUp me
if x and y = 1 then
alert "x and y are true"
else
if x = 1 and y = 0 then
alert"x=true, y= false"
else
if x = 0 and y = 1 then
alert "x= false, y= true"
else
alert "x and y are false"
end if
end if
end if
end

Resources
See Global variables (TechNote 14225) for more information about global variables and how they work.

Lingo
The following lingo commands were used to create the functionality in this sample movie. Please refer to the Lingo dictionary for more information about these terms: if, global, alert

Developer Notes
The sample file includes code comments and a cast member named "Developer Notes" which contains implementation details.

Note: This is just one of the many ways to create this functionality in Director.
This download contains the following file:

> if_statements.dir

Download the Windows zip file
Download the Macintosh hqx file


Last updated: March 28, 2002
Keywords: if, conditional statements, sample, how to, case
Created: March 1, 2002

©1995-2002 Macromedia, Inc. All rights reserved.
Use of this website signifies your agreement to the Terms of Use.
Privacy | Site Map
| Contact us | Accessibility